package com.feihong.yw189.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.feihong.yw189.R;
import com.feihong.yw189.adapter.HomeViewpagerAdapter;
import com.feihong.yw189.listener.http.RequestCallBack;
import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by PVer on 2017/8/18.
 * 首页的dialog显示
 */

public class HomeDialog extends Dialog {

    private ViewPager mViewPager;
    private ImageView  iv_close;
    private final String img_view;
    private final String id;
    private Context mContext;



    public HomeDialog(Context context, String  img, String aid) {
        //设置自定义Dialog的主题风格
        super(context, R.style.selfDefDialog);
        mContext =context;

        img_view = img;
        id = aid;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置Dialog的显示方式：alpha :0->1
        getWindow().setWindowAnimations(R.style.popupAnimation);
        //设置Dialog布局
        setContentView(R.layout.dialog_home_show_layout);
        setCanceledOnTouchOutside(false);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(createAdapter());
        iv_close= (ImageView) findViewById(R.id.tv_sure);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();

//                CityInfo cityInfo= UserLoginInfo.getUserCityInfo();
//
//                App.getApp().getHttpTools().homeTanChuangDialog(Integer.parseInt(id),HomeTanChuangDialog);

            }
        });
    }

    private PagerAdapter createAdapter() {
        List<View> adapterViews = new ArrayList<>();
//        for (int i = 1; i < 2; i++) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.homedialog_view_layout, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.tv_adapter_title);
        Glide.with(mContext).load(img_view.toString()).into(imageView);

        adapterViews.add(view);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent details=new Intent(mContext,MessageDetailsActivity.class);
//                details.putExtra("id",id);
//                details.putExtra(MessageDetailsActivity.MESSAGE_TYPE,MessageDetailsActivity.MESSAGE_LIST_TYPE);
//                mContext.startActivity(details);
                dismiss();
            }
        });

        return new HomeViewpagerAdapter(adapterViews);
    }


    RequestCallBack HomeTanChuangDialog = new RequestCallBack<String>() {
        @Override
        public void onSuccess(String result) {
            if(result == null){
                return;
            }
//            Suggestmodel  suggestmodel= JSON.parseObject(result,Suggestmodel.class);
//            if(suggestmodel != null && suggestmodel.getCode()== 200 ){
//                dismiss();
//            }else{
//                dismiss();
//            }
        }
        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
        }
        @Override
        public void onCancelled(Callback.CancelledException cex) {
        }
        @Override
        public void onFinished() {
        }
    };
}
